package com.mstore.action;

import java.util.HashMap;
import java.util.List;

import java.util.Map;

import com.mstore.pojo.Account;
import com.mstore.pojo.EnterpriseIntegralRule;
import com.mstore.service.EnterpriseIntegralRuleService;
import com.opensymphony.xwork2.ActionContext;

/**
 * 企业积分等级
 */
public class EnterpriseIntegralRuleAction extends BaseAction {

	private static final long serialVersionUID = -8949503781657222852L;

	private EnterpriseIntegralRule enterpriseIntegralRule;

	private EnterpriseIntegralRuleService enterpriseIntegralRuleService;

	private List<EnterpriseIntegralRule> enterpriseIntegralRules;

	private Map<String, Object> session = ActionContext.getContext()
			.getSession();

	private String result;

	/**
	 * 积分下限
	 */
	private Integer minValue;

	/**
	 * 积分上限
	 */
	private Integer maxValue;

	private Long ruleId;

	/**
	 * 添加积分等级
	 */
	public String addEnterpriseIntegralRule() {

		if (enterpriseIntegralRule == null) {
			return "addSuccess";
		}

		Object object = session.get("account");
		if (object == null) {
			result = "系统异常，请重新登陆！";
			return "error";
		}
		Account account = (Account) object;
		enterpriseIntegralRule.setEaId(account.getId());

		enterpriseIntegralRule.setCreateTime(getCreateTime());

		enterpriseIntegralRuleService
				.saveEnterpriseIntegralRule(enterpriseIntegralRule);

		return "addSuccess";
	}

	/**
	 * 查看积分等级规则
	 */
	public String watchEnterpriseIntegralRule() {

		Object object = session.get("account");
		if (object == null) {
			result = "系统异常，请重新登陆！";
			return "error";
		}
		Account account = (Account) object;
		enterpriseIntegralRule = new EnterpriseIntegralRule();
		enterpriseIntegralRule.setEaId(account.getId());

		enterpriseIntegralRules = enterpriseIntegralRuleService
				.watchEnterpriseIntegralRule(enterpriseIntegralRule);

		return "toWatchEnterpriseIntegralRule";
	}

	/**
	 * 跳转到修改积分等级规则页面
	 */
	public String initModifyIntegralrule() {
		enterpriseIntegralRule = enterpriseIntegralRuleService
				.getOneEnterpriseIntegralRule(enterpriseIntegralRule);
		return "toModifyIntegralrule";
	}

	/**
	 * 修改积分等级规则
	 */
	public String modifyIntegralrule() {
		enterpriseIntegralRuleService
				.modifyEnterpriseIntegralRule(enterpriseIntegralRule);
		return "modifySuccess";
	}

	/**
	 * 删除积分等级规则
	 */
	public String delEnterpriseIntegralRule() {
		enterpriseIntegralRuleService
				.delEnterpriseIntegralRule(enterpriseIntegralRule);
		return "delSuccess";
	}

	/**
	 * 验证积分等级上下限是否合理
	 */
	public String verifyIntegralRule() {
		Object object = session.get("account");
		if (object == null) {
			result = "系统异常，请重新登陆！";
			return "error";
		}
		Account account = (Account) object;
		enterpriseIntegralRule = new EnterpriseIntegralRule();
		enterpriseIntegralRule.setEaId(account.getId());

		if (ruleId == null) {
			enterpriseIntegralRules = enterpriseIntegralRuleService
					.watchEnterpriseIntegralRule(enterpriseIntegralRule);
		}
		else {
			enterpriseIntegralRule.setId(ruleId);
			enterpriseIntegralRules = enterpriseIntegralRuleService
					.getEnterpriseIntegralRuleWithoutSomeId(enterpriseIntegralRule);
		}

		Map<String, Object> map = new HashMap<String, Object>();
		boolean error = false;
		for (EnterpriseIntegralRule rule : enterpriseIntegralRules) {
			if (minValue >= rule.getMinValue() && minValue < rule.getMaxValue()) {
				error = true;
				break;
			}
			if (maxValue > rule.getMinValue() && maxValue <= rule.getMaxValue()) {
				error = true;
				break;
			}
		}

		if (error) {
			map.put("temp", "error");
			result = super.mapTojson(map);
			return "verifyIntegralRule";
		}

		map.put("temp", "ok");
		result = super.mapTojson(map);

		return "verifyIntegralRule";
	}

	public EnterpriseIntegralRule getEnterpriseIntegralRule() {
		return enterpriseIntegralRule;
	}

	public void setEnterpriseIntegralRule(
			EnterpriseIntegralRule enterpriseIntegralRule) {
		this.enterpriseIntegralRule = enterpriseIntegralRule;
	}

	public EnterpriseIntegralRuleService getEnterpriseIntegralRuleService() {
		return enterpriseIntegralRuleService;
	}

	public void setEnterpriseIntegralRuleService(
			EnterpriseIntegralRuleService enterpriseIntegralRuleService) {
		this.enterpriseIntegralRuleService = enterpriseIntegralRuleService;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public List<EnterpriseIntegralRule> getEnterpriseIntegralRules() {
		return enterpriseIntegralRules;
	}

	public void setEnterpriseIntegralRules(
			List<EnterpriseIntegralRule> enterpriseIntegralRules) {
		this.enterpriseIntegralRules = enterpriseIntegralRules;
	}

	public Integer getMinValue() {
		return minValue;
	}

	public void setMinValue(Integer minValue) {
		this.minValue = minValue;
	}

	public Integer getMaxValue() {
		return maxValue;
	}

	public void setMaxValue(Integer maxValue) {
		this.maxValue = maxValue;
	}

	public Long getRuleId() {
		return ruleId;
	}

	public void setRuleId(Long ruleId) {
		this.ruleId = ruleId;
	}

}
